package com.sharelords.demo.facade.impl;

import com.sharelords.biz.page.Page;
import com.sharelords.demo.biz.DemoTestBiz;
import com.sharelords.demo.biz.DemoTestRoleBiz;
import com.sharelords.demo.biz.bo.DemoTestBo;
import com.sharelords.demo.biz.bo.DemoTestRoleBo;
import com.sharelords.demo.biz.param.ConditionParamBo;
import com.sharelords.demo.client.DemoTestClient;
import com.sharelords.demo.client.ao.DemoTestAo;
import com.sharelords.demo.client.param.ConditionParamAo;
import com.sharelords.demo.common.base.client.impl.BaseFacadeImpl;
import com.sharelords.demo.common.enums.ReturnCodeEnum;
import com.sharelords.demo.common.result.ServiceResult;
import com.sharelords.demo.common.result.helper.ServiceResultHelper;
import com.sharelords.demo.common.util.InvokeUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class DemoTestFacadeImpl extends BaseFacadeImpl<DemoTestAo, DemoTestBo, Long, DemoTestBiz> implements DemoTestClient {

    private static final Logger logger = LoggerFactory.getLogger(DemoTestFacadeImpl.class);

    @Autowired
    private DemoTestBiz demoTestBiz;

    @Autowired
    private DemoTestRoleBiz demoTestRoleBiz;

    @Override
    public ServiceResult<DemoTestAo> queryByPrimaryKey(Long id) {
        if (id == null) {
            return ServiceResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            // 查询
            DemoTestBo bo = demoTestBiz.queryByPrimaryKey(id);

            // 结果转换及返回
            return ServiceResultHelper.success(InvokeUtil.copy(bo, DemoTestAo.class));
        } catch (Exception e) {
            logger.error("根据主键获取详情发生异常，id={}，异常信息为：{}", id, e);
            return ServiceResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ServiceResult<DemoTestAo> queryByName(String name) {
        if (StringUtils.isBlank(name)) {
            return ServiceResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            // 查询
            DemoTestBo bo = demoTestBiz.queryByName(name);

            // 结果转换及返回
            return ServiceResultHelper.success(InvokeUtil.copy(bo, DemoTestAo.class));
        } catch (Exception e) {
            logger.error("根据姓名获取详情发生异常，name={}，异常信息为：{}", name, e);
            return ServiceResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ServiceResult<List<DemoTestAo>> queryPageByConditions(ConditionParamAo paramAo, int pageNum, int pageSize) {
        if (paramAo == null || pageNum < 0 || pageSize <= 0) {
            return ServiceResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            // 数据组装
            // 查询条件
            ConditionParamBo paramBo = InvokeUtil.copy(paramAo, ConditionParamBo.class);
            // 分页条件
            Page page = new Page();
            page.setPageNum(pageNum);
            page.setPageSize(pageSize);
            // 查询
            List<DemoTestBo> boList = demoTestBiz.queryPageByConditions(paramBo, page);
            if (boList == null || boList.isEmpty()) {
                return ServiceResultHelper.success(Collections.emptyList());
            }

            // 角色id列表
            List<Integer> roleIdList = InvokeUtil.transListNoRepeat(boList, new InvokeUtil.Child<Integer, DemoTestBo>() {
                @Override
                public Integer group(DemoTestBo demoTestBo) {
                    return demoTestBo.getRole();
                }
            });
            if (roleIdList.isEmpty()) {
                return ServiceResultHelper.success(InvokeUtil.copyList(boList, DemoTestAo.class));
            }

            // 角色信息获取
            List<DemoTestRoleBo> roleBoList = demoTestRoleBiz.queryReCordsByIdList(roleIdList);
            if (roleBoList == null || roleBoList.isEmpty()) {
                return ServiceResultHelper.success(InvokeUtil.copyList(boList, DemoTestAo.class));
            }
            final Map<Integer, DemoTestRoleBo> roleMap = InvokeUtil.transList2Map(roleBoList, new InvokeUtil.ChildForMap<Integer, DemoTestRoleBo, DemoTestRoleBo>() {
                @Override
                public InvokeUtil.SimpleEntry<Integer, DemoTestRoleBo> group(DemoTestRoleBo demoTestRoleBo) {
                    return new InvokeUtil.SimpleEntry<>(demoTestRoleBo.getId(), demoTestRoleBo);
                }
            });

            // 角色信息封装
            List<DemoTestAo> demoTestAoList = InvokeUtil.transList(boList, new InvokeUtil.Child<DemoTestAo, DemoTestBo>() {
                @Override
                public DemoTestAo group(DemoTestBo demoTestBo) {
                    DemoTestAo demoTestAo = InvokeUtil.copy(demoTestBo, DemoTestAo.class);

                    // 角色名称封装
                    DemoTestRoleBo demoTestRoleBo = roleMap.get(demoTestBo.getRole());
                    if (demoTestRoleBo != null) {
                        demoTestAo.setRoleName(demoTestRoleBo.getName());
                    }

                    return demoTestAo;
                }
            });

            // 结果转换及返回
            return ServiceResultHelper.success(demoTestAoList);
        } catch (Exception e) {
            logger.error("多条件分页查询数据列表发生异常，paramAo={}，pageNum={}，pageSize={}，异常信息为：{}", paramAo, pageNum, pageSize, e);
            return ServiceResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ServiceResult<Integer> queryCountByConditions(ConditionParamAo paramAo) {
        if (paramAo == null) {
            return ServiceResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            // 查询
            Integer count = demoTestBiz.queryCountByConditions(InvokeUtil.copy(paramAo, ConditionParamBo.class));

            // 结果转换及返回
            return ServiceResultHelper.success(count);
        } catch (Exception e) {
            logger.error("多条件分页查询记录条数发生异常，paramAo={}，异常信息为：{}", paramAo, e);
            return ServiceResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }
}
